function [total_cost, cost_breakdown] = grid_objective(x, grid_data)
% 电网调度目标函数
% 计算总运行成本，包括发电成本、网络损耗、碳排放成本等
%
% INPUTS:
%   x - 决策变量向量 [发电机出力, 储能充放电, 负荷分配]
%   grid_data - 电网模型数据
%
% OUTPUTS:
%   total_cost - 总运行成本
%   cost_breakdown - 成本分解

% 解析决策变量
num_gen = grid_data.num_generators;
num_storage = grid_data.num_storage;
num_loads = grid_data.num_loads;
time_periods = grid_data.time_periods;

% 变量索引定义
gen_start = 1;
gen_end = num_gen * time_periods;
storage_start = gen_end + 1;
storage_end = storage_start + num_storage * time_periods - 1;
load_start = storage_end + 1;
load_end = load_start + num_loads * time_periods - 1;

% 提取各时段发电机出力
gen_power = reshape(x(gen_start:gen_end), num_gen, time_periods);

% 提取各时段储能充放电功率（正值充电，负值放电）
storage_power = reshape(x(storage_start:storage_end), num_storage, time_periods);

% 提取各时段负荷分配
load_allocation = reshape(x(load_start:load_end), num_loads, time_periods);

% 初始化成本分解
cost_breakdown = struct();
cost_breakdown.generation_cost = 0;
cost_breakdown.network_loss = 0;
cost_breakdown.carbon_cost = 0;
cost_breakdown.storage_cost = 0;
cost_breakdown.violation_penalty = 0;

%% 1. 发电成本计算
for t = 1:time_periods
    for g = 1:num_gen
        power = gen_power(g, t);
        if power > 0
            % 二次函数成本模型: cost = a + b*P + c*P^2
            gen = grid_data.generators(g);
            cost = gen.cost_a + gen.cost_b * power + gen.cost_c * power^2;
            cost_breakdown.generation_cost = cost_breakdown.generation_cost + cost;
        end
    end
end

%% 2. 网络损耗计算（简化模型）
for t = 1:time_periods
    % 计算各节点净注入功率
    node_injection = zeros(grid_data.num_nodes, 1);
    
    % 发电机注入
    for g = 1:num_gen
        node = grid_data.generators(g).node;
        node_injection(node) = node_injection(node) + gen_power(g, t);
    end
    
    % 负荷消耗
    for l = 1:num_loads
        node = grid_data.loads(l).node;
        base_load = grid_data.loads(l).base_load * grid_data.loads(l).load_profile(t);
        allocated_load = load_allocation(l, t) * base_load;
        node_injection(node) = node_injection(node) - allocated_load;
    end
    
    % 储能设备
    for s = 1:num_storage
        node = grid_data.storage(s).node;
        node_injection(node) = node_injection(node) - storage_power(s, t);
    end
    
    % 新能源注入
    solar_power = grid_data.renewable.solar_capacity * grid_data.renewable.solar_profile(t);
    wind_power = grid_data.renewable.wind_capacity * grid_data.renewable.wind_profile(t);
    % 假设新能源连接到前几个节点
    node_injection(1) = node_injection(1) + solar_power * 0.6;
    node_injection(2) = node_injection(2) + solar_power * 0.4;
    node_injection(3) = node_injection(3) + wind_power * 0.7;
    node_injection(4) = node_injection(4) + wind_power * 0.3;
    
    % 简化网络损耗计算（基于节点注入功率的平方和）
    loss_factor = 0.02; % 2%的损耗率
    total_injection = sum(abs(node_injection));
    cost_breakdown.network_loss = cost_breakdown.network_loss + loss_factor * total_injection;
end

%% 3. 碳排放成本计算
carbon_price = 50; % 碳价格（元/吨CO2）
for t = 1:time_periods
    for g = 1:num_gen
        power = gen_power(g, t);
        if power > 0
            gen = grid_data.generators(g);
            carbon_emission = power * gen.carbon_factor; % 吨CO2
            cost_breakdown.carbon_cost = cost_breakdown.carbon_cost + carbon_emission * carbon_price;
        end
    end
end

%% 4. 储能设备成本
storage_cost_rate = 10; % 储能充放电成本（元/MWh）
for t = 1:time_periods
    for s = 1:num_storage
        power = abs(storage_power(s, t));
        if power > 0
            cost_breakdown.storage_cost = cost_breakdown.storage_cost + power * storage_cost_rate;
        end
    end
end

%% 5. 约束违反惩罚
penalty_factor = 1000; % 惩罚因子

% 发电机出力约束
for t = 1:time_periods
    for g = 1:num_gen
        power = gen_power(g, t);
        gen = grid_data.generators(g);
        
        % 出力上下限约束
        if power < gen.min_power
            cost_breakdown.violation_penalty = cost_breakdown.violation_penalty + ...
                penalty_factor * (gen.min_power - power)^2;
        end
        if power > gen.max_power
            cost_breakdown.violation_penalty = cost_breakdown.violation_penalty + ...
                penalty_factor * (power - gen.max_power)^2;
        end
        
        % 爬坡约束
        if t > 1
            ramp_change = power - gen_power(g, t-1);
            if ramp_change > gen.ramp_up
                cost_breakdown.violation_penalty = cost_breakdown.violation_penalty + ...
                    penalty_factor * (ramp_change - gen.ramp_up)^2;
            end
            if ramp_change < -gen.ramp_down
                cost_breakdown.violation_penalty = cost_breakdown.violation_penalty + ...
                    penalty_factor * (-ramp_change - gen.ramp_down)^2;
            end
        end
    end
end

% 储能设备约束
for t = 1:time_periods
    for s = 1:num_storage
        power = storage_power(s, t);
        storage = grid_data.storage(s);
        
        % 功率约束
        if abs(power) > storage.max_power
            cost_breakdown.violation_penalty = cost_breakdown.violation_penalty + ...
                penalty_factor * (abs(power) - storage.max_power)^2;
        end
    end
end

% 功率平衡约束
for t = 1:time_periods
    total_gen = sum(gen_power(:, t));
    total_load = sum(load_allocation(:, t) .* ...
        arrayfun(@(i) grid_data.loads(i).base_load * grid_data.loads(i).load_profile(t), 1:num_loads));
    total_storage = sum(storage_power(:, t));
    
    solar_power = grid_data.renewable.solar_capacity * grid_data.renewable.solar_profile(t);
    wind_power = grid_data.renewable.wind_capacity * grid_data.renewable.wind_profile(t);
    total_renewable = solar_power + wind_power;
    
    power_balance = total_gen + total_renewable - total_load - total_storage;
    
    if abs(power_balance) > 1 % 允许1MW的误差
        cost_breakdown.violation_penalty = cost_breakdown.violation_penalty + ...
            penalty_factor * power_balance^2;
    end
end

%% 6. 计算总成本
total_cost = cost_breakdown.generation_cost + ...
             cost_breakdown.network_loss + ...
             cost_breakdown.carbon_cost + ...
             cost_breakdown.storage_cost + ...
             cost_breakdown.violation_penalty;

end

